﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Modle.Home;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Products.Components.Category;
using RiderShop.Mobile.ServiceEntity.Store;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Promotion;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Service.Entity.Weixin;

namespace RiderShop.Mobile.Modle.Product
{
    public class ProductListModel
    {
        public string Keyword { get; set; }

        public int SubcategoryCode { get; set; }

        public int FiterIndex { get; set; }

        public AdvertisementImageExtend ProductBanner { get; set; }
        /*
        <option value="2">商品排序:人气最旺</option>
        <option value="3">商品排序:价格从低到高</option>
        <option value="4">商品排序:价格从高到低</option>
        <option value="5">商品排序:最近上架</option>
         */
        public Dictionary<string, int> SearchCondition
        {
            get
            {
                var list = new Dictionary<string, int>();
                list.Add("商品排序:销量最好", 1);
                list.Add("商品排序:人气最旺", 2);
                list.Add("商品排序:价格从低到高", 3);
                list.Add("商品排序:价格从高到低", 4);
                list.Add("商品排序:最近上架", 5);
                return list;
            }
        }


        public DbQueryResult<List<ProductBasicInfoExtend>> Result { get; set; }

        private int pageIndex = 0;
        public int PageIndex
        {
            get
            {
                return pageIndex;
            }
            set { pageIndex = value; }
        }

        public bool CheckProductExists(string productId)
        {
            List<ProductBasicInfoExtend> products = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfoDetailed", productId));

            return products.FirstOrDefault() != null;
        }

        /// <summary>
        /// 得到产品列表根据分类Id
        /// </summary>
        /// <returns></returns>
        public DbQueryResult<List<ProductBasicInfoExtend>> GetProducts(int categoryId)
        {
            SubcategoryCode = categoryId;
            var query = new ProductQuery();
            query.CategoryId = categoryId;
            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);
            Result = productList;

            return productList;
        }
        /// <summary>
        /// 
        /// </summary>
        public static List<ProductBasicInfoExtend> GetHotProducts(int count = 4)
        {
            ProductQuery query = new ProductQuery()
            {
                PageIndex = 0,
                PageSize = count,
                SortBy = ProductQuery.ProductSortColumn.TotalSoldQty.ToString(),
                SortOrder = SortAction.Desc,
                IsWebsiteSearch = true
            };
            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);
            return productList.Data ?? new List<ProductBasicInfoExtend>();
        }

        public static List<ProductBasicInfoExtend> GetSuggestProducts()
        {
            ProductQuery query = new ProductQuery()
            {
                PageIndex = 0,
                PageSize = 10,
                SortBy = ProductQuery.ProductSortColumn.ActiveDate.ToString(),
                SortOrder = SortAction.Desc,
                IsWebsiteSearch = true
            };
            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);
            if (productList.Data != null && productList.Data.Count >= 10)
            {
                return productList.Data.Skip((new Random()).Next(0, 6)).Take(3).ToList();
            }
            else if (productList.Data != null && productList.Data.Count > 3)
            {
                return productList.Data.Take(3).ToList();
            }
            else
            {
                return new List<ProductBasicInfoExtend>();
            }
        }

        /// <summary>
        /// 得到预售商品列表
        /// </summary>
        /// <returns>得到预售商品列表</returns>
        public static List<ProductBasicInfoExtend> GetPresellProducts()
        {
            ProductQuery query = new ProductQuery()
            {
                PageIndex = 0,
                PageSize = 50,
                SortBy = ProductQuery.ProductSortColumn.ActiveDate.ToString(),
                SortOrder = SortAction.Desc,
                PromotionType = ProductPromoType.Presell,
                IsWebsiteSearch = true
            };

            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);

            if (productList.Data != null && productList.Data.Count > 0)
            {
                return productList.Data;
            }
            else
            {
                return new List<ProductBasicInfoExtend>();
            }
        }

        /// <summary>
        /// 得到限时特价的商品
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>得到限时特价的商品</returns>
        public void GetSpecialPriceProducts(GroupBuyProductQuery query)
        {
            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("SalesManagement_GetGroupByPrdocut"), query);
            Result = productList;
            //分页信息
            PageSize = query.PageSize;
            PageIndex = query.PageIndex;
            TotalCount = productList.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)PageSize);

            //Kevin.Comm.Logger.Tracer.TraceLog("test: TotalCount:" + TotalCount + "-PageSize:" + PageSize + "-TotalPage:" + TotalPage, "Template Message Job", "TemplateMessageJob", true);

        }

        /// <summary>
        /// 提醒我
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="notifyDate">通知时间</param>
        public ServiceResponser ReminderMe(string productId, string notifyDate, string productName)
        {
            var request = new WeixinNotifyEntity()
            {
                ProductId = productId,
                UserId = WebClientContext.Current.UserId,
                NotifyType = 1,
                NotifyDate =  Convert.ToDateTime(notifyDate).AddMinutes(-10),
                Status = 0,
                ProductName = productName,
                YuYueDate = DateTime.Now
            };
            string url = SiteUrl.FormatUrl("SalesManagement_ReminderMe");
            var result = RestfulHelper.Update(url, request);

            return result;
        }
        
        /// <summary>
        /// 得到产品列表根据分类Id
        /// </summary>
        /// <returns></returns>
        public DbQueryResult<List<ProductBasicInfoExtend>> GetProducts(ProductQuery query)
        {
            SubcategoryCode = query.CategoryId;
            DbQueryResult<List<ProductBasicInfoExtend>> productList = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);
            Result = productList;
            //分页信息
            PageSize = query.PageSize;
            PageIndex = query.PageIndex;
            TotalCount = productList.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)PageSize);
            HomeBannerViewModel banner = new HomeBannerViewModel();
            ProductBanner = banner.HomeTop04Images.FirstOrDefault();

            if (query.CategoryId > 1)
            {
                var categoryInfos = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", query.CategoryId));
                if (categoryInfos.Count > 0)
                {
                    Category = categoryInfos[0];
                    if (Category.Depth == CategoryLevelType.SubCategory)
                    {
                        categoryInfos = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", Category.ParentCategoryId));
                        if (categoryInfos.Count > 0)
                        {
                            Category = categoryInfos[0];
                        }
                        //Category = new CategoryInfo() { CategoryName = Category.ParentCategoryName, CategoryId = Category.ParentCategoryId.Value };
                    }
                }
            }
            else
            {
                Category = new CategoryInfo() { CategoryName = "全部商品", CategoryId = 0 };
            }

            return productList;
        }

        //是否有上一页
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }

        //是否有下一页
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPage); }
        }

        //每页记录的数量
        private int _pageSize;
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        //总记录数
        private int _totalCount;
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }

        //总页数
        private int _totalPage;
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }

        public CategoryInfo Category { get; set; }
    }
}
